<template>
  <div class="page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品添加</el-breadcrumb-item>
    </el-breadcrumb>
    <div class=" table animate__animated animate__bounceInLeft">
      <el-form ref="form" :model="form" label-width="80px" >
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsName"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="form.goodsPrice"></el-input>
        </el-form-item>

        <el-form-item label="商品图片"></el-form-item>
        <el-upload
          action="https://jsonplaceholder.typicode.com/posts/"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-success="handlePictureSuccess"
          :before-upload="beforeAvatarUpload"
          :on-remove="handleRemove"
          :headers="{ 'Content-Type': 'application/x-www-form-urlencoded' }"
          :limit="1"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
        <!-- 上传图片 -->

        <el-form-item label="商品简介" class="goodsinfo">
          <el-input
            type="textarea"
            v-model="form.goodsIntro"
            class="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即添加</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        goodsName: "",
        goodsPrice: "",
        goodsIntro: "",
      },
      dialogImageUrl: "",
      dialogVisible: false,
      file: {},
    };
  },
  methods: {
    onSubmit() {
      var file = this.file;
      var param = new FormData();

      //上传文件 和其他数据
      param.append("filename", file);
      param.append("goodsName", this.form.goodsName);
      param.append("goodsPrice", this.form.goodsPrice);
      param.append("goodsIntro", this.form.goodsIntro);

      //   console.log(this.form);

      this.$axios
        .post("/api/store/add", param, {
          headers: { "Content-Type": "multipart/form-data" },
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.$message({
              message: "恭喜你，添加商品成功",
              type: "success",
            });
          } else {
            this.$message({
              message: "添加失败，请重新添加code=1",
              type: "error",
            });
          }
        })
        .catch((err) => {
          this.$message({
            message: "添加失败，请重新添加 err",
            type: "error",
          });
        });
    },
    handleRemove(file, fileList) {
      // console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handlePictureSuccess(response, file, fileList) {
      this.file = file.raw;
      this.dialogImageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/png";
      const isLt2M = file.size / 1024 / 1024 < 8;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG或者 PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  //   height: 100%;
  // margin: 0 auto;
}
.table {
margin:30px auto;
  padding: 60px;
  width: 60%;
  background: white;
  opacity: 0.8;
}
.textarea {
  min-height: 60px;
}
.goodsinfo {
  margin-top: 20px;
}
</style>
